/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yutel.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author yuanyajun
 */
public class DBUtil {

    private RecordStore recordStore;
    public static final String RNAME = "dbName";

    public DBUtil() {
        try {
            recordStore = RecordStore.openRecordStore(RNAME, true);
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }

    public void closeRecordStore() {
        try {
            recordStore.closeRecordStore();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }
    }

    public void saveAccount(Account account, int recordID) {
        Account a = getAccount(recordID);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            account.serialize(dos);
            byte[] data = baos.toByteArray();
            if (a == null) {
                recordStore.addRecord(data, 0, data.length);
            } else {
                recordStore.setRecord(recordID, data, 0, data.length);
            }
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }

    }

    public Account getAccount(int recordID) {
        try {
            if (recordStore.getNumRecords() > 0) {
                byte[] data = recordStore.getRecord(recordID);
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                Account account = Account.deserialize(dis);
                bais.close();
                return account;
            }
            return null;
        } catch (IOException e) {
            return null;
        } catch (RecordStoreException e) {
            return null;
        }
    }

    public void savePreference(Preference preference, int recordID) {
        Preference p = getPreference(recordID);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
            preference.serialize(dos);
            byte[] data = baos.toByteArray();
            if (p == null) {
                recordStore.addRecord(data, 0, data.length);
            } else {
                recordStore.setRecord(recordID, data, 0, data.length);
            }
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (RecordStoreException e) {
            e.printStackTrace();
        }

    }

    public Preference getPreference(int recordID) {
        try {
            if (recordStore.getNumRecords() > 0) {
                byte[] data = recordStore.getRecord(recordID);
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dis = new DataInputStream(bais);
                Preference preference = Preference.deserialize(dis);
                bais.close();
                return preference;
            }
            return null;
        } catch (IOException e) {
            return null;
        } catch (RecordStoreException e) {
            return null;
        }
    }
}
